package com.example2016.cjj.wxplangamedemo.game2;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;

import com.example2016.cjj.wxplangamedemo.view.GameSurfaceView;

/**
 * Created by CJJ on 2017/3/26.
 *
 * Sprite类，即精灵类，游戏中的飞机、子弹、奖励道具等都是继承自该类，
 * 我们通过moveTo()、move()等方法控制精灵的位置，通过beforeDraw()、onDraw()、afterDraw()实现相应的绘图逻辑。
 */

public class Sprite {

    private Bitmap bitmap=null; //精灵的图片

    private float x=0,y=0; //x，y的值

    private int frame = 0; //绘制的次数

    private boolean isDestroy = false; //销毁标识

    private boolean visible = true; //是否可见标识

    private float collideOffset = 0; //碰撞Rect的偏移量


    public Sprite(Bitmap bitmap){
        this.bitmap = bitmap;
    }


    //绘制方法
    public final void  draw(Canvas canvas, Paint paint, GameSurfaceView gameView){
        frame ++;
        beforeDraw(canvas,paint,gameView);
        onDraw(canvas,paint,gameView);
        afterDraw(canvas,paint,gameView);
    }

    //绘制前
    protected void beforeDraw(Canvas canvas, Paint paint, GameSurfaceView gameView) {

    }

    //绘制中
    public void onDraw(Canvas canvas, Paint paint, GameSurfaceView gameView) {
        if( !isDestroy && bitmap!=null && getVisibility() ){
            //将Sprite绘制到Canvas上
            Rect srcRect = getBitmapSrcRect();
            RectF dstRectF = getRectf();
            canvas.drawBitmap(bitmap,srcRect,dstRectF,paint);

        }

    }

    //绘制后
    protected void afterDraw(Canvas canvas, Paint paint, GameSurfaceView gameView) {

    }




    //移动偏移量
    public void  move(float offsetX,float offsetY){
        x +=offsetX;
        y +=offsetY;
    }

    //移动到x,y
    public void  moveTo(float x,float y){
        this.x = x;
        this.y = y;
    }


    //中心移动到centerX,centerY
    public void  centerTo(float centerX,float centerY){
        this.x = centerX - getWidth()/2;
        this.y = centerY - getHeight()/2;
    }


    //获取 目标位置的坐标
    public RectF getRectf(){
        RectF rectF = new RectF();
        rectF.left = x;
        rectF.top = y;
        rectF.right = rectF.left + getWidth();
        rectF.bottom = rectF.top + getHeight();

        return  rectF;
    }

    //获取 图片自身的Rect
    public Rect getBitmapSrcRect(){
        Rect rect = new Rect();
        rect.left = 0;
        rect.top = 0;
        rect.right = (int) getWidth();
        rect.bottom = (int) getHeight();

        return rect;
    }


    //获取碰撞的Rect，考虑存在碰撞偏移量
    public RectF getCollideRectF(){
        RectF rectF = getRectf();
        rectF.left -= collideOffset;
        rectF.top  -= collideOffset;
        rectF.right +=collideOffset;
        rectF.bottom +=collideOffset;

        return rectF;
    }

    //判断与其他Sptrite是否有碰撞点
    public Point getCollidePointWithOther(Sprite sprite){
        Point p =null;
        RectF SelfRectF = getCollideRectF();
        RectF spriteRect = sprite.getCollideRectF();

        RectF rectF = new RectF();
        boolean isIntersect = rectF.setIntersect(SelfRectF,spriteRect);
        if( isIntersect ){
            p = new Point( Math.round(rectF.centerX()), Math.round(rectF.centerY()) );
        }

        return p;
    }



    //获取width
    public float getWidth(){
        if( bitmap !=null){
            return  bitmap.getWidth();
        }
        return 0;
    }

    //获取height
    public float getHeight(){
        if( bitmap!=null ){
            return bitmap.getHeight();
        }
        return 0;
    }

    //销毁
    public void destroy(){
        bitmap =null;
        isDestroy = true;
    }



    public Bitmap getBitmap() {
        return bitmap;
    }

    public void setBitmap(Bitmap bitmap) {
        this.bitmap = bitmap;
    }

    public float getX() {
        return x;
    }

    public void setX(float x) {
        this.x = x;
    }

    public float getY() {
        return y;
    }

    public void setY(float y) {
        this.y = y;
    }

    public int getFrame() {
        return frame;
    }

    public void setFrame(int frame) {
        this.frame = frame;
    }

    public boolean isDestroy() {
        return isDestroy;
    }

    public void setDestroy(boolean destroy) {
        isDestroy = destroy;
    }

    public void setVisibility(boolean visible){
        this.visible = visible;
    }

    public boolean getVisibility(){
        return visible;
    }

}
